package samples;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class S02_PrintLess200Via2Threads {
    private static final AtomicInteger counter = new AtomicInteger(0);
    private static final AtomicBoolean stop = new AtomicBoolean(false);

    public static void test() {
        Thread thread01 = new Thread(()->{
            while ( counter.get() < 200) {
                if (stop.get()) {
                    System.out.println(Thread.currentThread() + ": " + counter.getAndIncrement());
                    stop.compareAndSet(stop.get(), !stop.get());
                }
            }
        });

        Thread thread02 = new Thread(()->{
            while (counter.get() < 200) {
                if (!stop.get()) {
                    System.out.println(Thread.currentThread() + ": " + counter.getAndIncrement());
                    stop.compareAndSet(stop.get(), !stop.get());
                }
            }
        });

        thread01.start();
        thread02.start();
    }
}
